var util = require('../../utils/util.js');

// pages/focusOn1/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isActive: 0, // tab切换索引
    touristPage: 0, // 行游家分页页码
    linePage: 0, // 线路分页页码
    touristLimit: 10, // 行游家单页数据个数
    lineLimit: 10, // 线路单页数据个数
    touristList: [], // 行游家数据列表 
    rLineList: [], // 线路数据列表
    lLineList: [], // 线路数据列表
    touristSwitch: true, // 上拉加载数据开关
    lineSwitch: true, // 上拉加载数据开关
    btTips: '', // 底部提示,
    state: ''
  },
  /**
   * tab切换
   */
  isActive:function(e){
    this.setData({
      isActive:e.target.dataset.num,
    })
    if(this.data.isActive == 0){
      this.getFocusTourist();
    } else {
      this.getLine();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFocusTourist();
  },

  /**
   * 获取关注的行游家
   */
  getFocusTourist: function (){
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    util.http({
      url: 'api/miniwechat/center/trave_home',
      headers: {
        token: userInfo.token
      },
      data: {
        user_id: userInfo.user_id,
        offset: this.data.touristPage,
        limit: this.data.touristLimit
      }
    }).then(res => {
      if(res.total > 0) {
        let list = that.data.touristList
        let maxLen = (that.data.touristPage + 1) * that.data.touristLimit
        if (list.length < maxLen && list.length < res.total) {
          res.rows.forEach((item, index) => {
            list.push(item)
          })
        }
        let touristSwitch = list.length == res.total ? false : true
        let tips = touristSwitch ? '上拉加载更多' : '暂无更多数据'
        that.setData({
          touristList: list,
          btTips: tips,
          touristSwitch: touristSwitch
        });
      } else {
        that.setData({
          touristList: [],
          btTips: '暂无更多数据',
          touristSwitch: false
        });
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    }).catch(ex => {
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    })
  },
  /**
   * 获取收藏的线路
   */
  getLine: function(){
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    util.http({
      url: 'api/miniwechat/center/coll',
      data: {
        user_id: userInfo.user_id,
        offset: this.data.linePage,
        limit: this.data.lineLimit
      }
    }).then(res => {
      if (res.total > 0) {
        let larr = that.data.lLineList, rarr = that.data.rLineList
        let maxLen = (that.data.linePage + 1) * that.data.lineLimit
        if ((larr.length + rarr.length) < maxLen && (larr.length + rarr.length) < res.total) {
          res.rows.forEach((item, index) => {
            if (!item.line_time_list){
              return;
            }
            let beginTime = item.line_time_list.begin_time
            let endTime = item.line_time_list.end_time
            let totalTime = (new Date(endTime).getTime() - new Date(beginTime).getTime()) / (1*24*60*60*1000)
            let surplusNum = item.line_time_list.quota - item.line_time_list.number
            item.line_time_list.totalTime = totalTime
            item.line_time_list.begin_time = timeReset(beginTime)
            item.line_time_list.end_time = timeReset(endTime)
            item.line_time_list.surplusNum = surplusNum
            that.setData({
              state: item.line_time_list.status
            })
            switch (item.line_time_list.status) {
              case 0:
                item.line_time_list.status = '已取消'
                break;
              case 1:
                item.line_time_list.status = '报名中'
                break;
              case 2:
                item.line_time_list.status = '报名结束'
                break;
              case 3:
                item.line_time_list.status = '已完成'
                break;
            }
            (index+1) % 2 == 0 ? rarr.push(item) : larr.push(item);
          })
        }
        let lineSwitch = (larr.length + rarr.length) == res.total ? false : true;
        let tips = lineSwitch ? '上拉加载更多' : '暂无更多数据'
        that.setData({
          rLineList: rarr,
          lLineList: larr,
          btTips: tips,
          lineSwitch: lineSwitch
        });
      } else {
        that.setData({
          rLineList: [],
          rLineList: [],
          btTips: '暂无更多数据',
          lineSwitch: false
        });
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    }).catch(ex => {
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    })
  },
  /**
   * 取消关注某行游家
   */
  onFollow (e) {
    let that = this
    let thisId = e.currentTarget.dataset.id
    util.http({
      url: 'api/miniwechat/index/follow',
      method: 'POST',
      data: {
        user_id: util.isLogin().user_id,
        admin_id: thisId,
        type: 0
      }
    }).then(res => {
      let touristList = that.data.touristList
      touristList.forEach((item, index) => {
        if (item.id == thisId) {
          touristList.splice(index, 1)
        }
      })
      that.setData({
        touristList: touristList
      })
      wx.showToast({
        title: '取消关注成功',
        icon: 'none',
        duration: 2000
      })
    }).catch(ex => {

    })
  },
  /**
   * 取消收藏某线路
   */
  onCollect(e) {
    let that = this
    let thisId = e.currentTarget.dataset.id
    util.http({
      url: 'api/miniwechat/index/coll',
      method: 'POST',
      data: {
        user_id: util.isLogin().user_id,
        line_id: thisId,
        type: 0
      }
    }).then(res => {
      let lLineList = that.data.lLineList
      let rLineList = that.data.rLineList
      let totalList = [...lLineList, ...rLineList]
      console.log(totalList)
      totalList.forEach((item, index) => {
        if (item.line_id == thisId) {
          totalList.splice(index, 1)
        }
      })
      lLineList = []
      rLineList = []
      totalList.forEach((item, index) => {
        (index + 1) % 2 == 0 ? rLineList.push(item) : lLineList.push(item);
      })
      that.setData({
        rLineList: rLineList,
        lLineList: lLineList
      })
      wx.showToast({
        title: '取消收藏成功',
        icon: 'none',
        duration: 2000
      })
    }).catch(ex => {

    })
  },
  /**
   * 行游家上拉加载数据
   */
  lower () {
    if (this.data.isActive == 0) {
      if (this.data.touristSwitch) {
        let page = this.data.touristPage + 1;
        this.setData({
          touristPage: page
        });
        wx.showLoading({
          title: '加载中',
        })
        this.getFocusTourist()
      }
    } else {
      if (this.data.lineSwitch) {
        let page = this.data.linePage + 1;
        this.setData({
          linePage: page
        });
        wx.showLoading({
          title: '加载中',
        })
        this.getLine()
      }
    }
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
/**
 * 日期格式重置
 */
function timeReset(time) {
  let arr = time.split("-")
  arr.splice(0, 1)
  arr = arr.join(".")
  return arr
}